package com.mdd.admin.controller.order;

import com.github.binarywang.wxpay.exception.WxPayException;
import com.mdd.admin.config.aop.Log;
import com.mdd.admin.service.order.IOrderRefundService;
import com.mdd.admin.validate.order.OrderRefundParam;
import com.mdd.admin.validate.common.PageParam;
import com.mdd.admin.vo.order.OrderLogListVo;
import com.mdd.admin.vo.order.OrderRefundListVo;
import com.mdd.admin.vo.order.OrderRefundDetailVo;
import com.mdd.admin.vo.order.OrderRefundLogListVo;
import com.mdd.common.core.AjaxResult;
import com.mdd.common.core.PageResult;
import com.mdd.common.validator.annotation.IDMust;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * 订单退款管理
 * @author panweiliang
 */
@RestController
@RequestMapping("/api/order/refund")
public class OrderRefundController {

    @Resource
    IOrderRefundService iOrderRefundService;

    /**
     * 订单退款列表
     *
     * @author panweiliang
     * @param pageParam 分页参数
     * @param params 搜索参数
     * @return Object
     */
    @GetMapping("/list")
    public Object list(@Validated PageParam pageParam,
                       @RequestParam Map<String, String> params) {
        PageResult<OrderRefundListVo> list = iOrderRefundService.list(pageParam, params);
        return AjaxResult.success(list);
    }

    /**
     * 订单退款日志列表
     *
     * @author panweiliang
     * @param refundId 订单id
     * @return Object
     */
    @GetMapping("/log/list")
    public Object logList(@RequestParam("refundId") Integer refundId) {
        List<OrderRefundLogListVo> list = iOrderRefundService.listLog(refundId);
        return AjaxResult.success(list);
    }

    /**
     * 退款订单不同状态的统计数据
     *
     * @author panweiliang
     * @return Object
     */
    @GetMapping("/statistics")
    public Object statistics() {
        Map<String, Object> map = iOrderRefundService.statistics();
        return AjaxResult.success(map);
    }

    /**
     * 订单退款新增
     *
     * @author panweiliang
     * @param orderRefundParam 参数
     * @return Object
     */
    @Log(title = "订单退款新增")
    @PostMapping("/add")
    public Object add(@Validated(value = OrderRefundParam.create.class) @RequestBody OrderRefundParam orderRefundParam) throws WxPayException {
        iOrderRefundService.add(orderRefundParam);
        return AjaxResult.success();
    }


    /**
     * 重新退款
     *
     * @author panweiliang
     * @param orderRefundParam 参数
     * @return Object
     */
    @Log(title = "重新退款")
    @PostMapping("/again")
    public Object addAgain(@Validated(value = OrderRefundParam.create.class) @RequestBody OrderRefundParam orderRefundParam) throws WxPayException {
        iOrderRefundService.addAgain(orderRefundParam);
        return AjaxResult.success();
    }

    /**
     * 订单退款删除
     *
     * @author panweiliang
     * @param orderRefundParam 参数
     * @return Object
     */
    @Log(title = "订单退款删除")
    @PostMapping("/del")
    public Object del(@Validated(value = OrderRefundParam.delete.class) @RequestBody OrderRefundParam orderRefundParam) {
        iOrderRefundService.del(orderRefundParam.getId());
        return AjaxResult.success();
    }

}
